package com.mall.web.control.eshop;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.mall.biz.ImageUploadBizImpl;
import com.mall.client.commkey.CommKeyService;
import com.mall.client.eshop.EshopBannerService;
import com.mall.common.util.PageUtil;
import com.mall.common.util.bit.PropertiesConfigure;
import com.mall.constant.PropertiesKey;
import com.mall.enums.RegionEnum;
import com.mall.pojo.Commkey;
import com.mall.pojo.EshopBanner;
import com.mall.query.Page;
import com.mall.web.util.HttpUtil;
import com.mall.web.vo.ResponseEntity;

@Controller
@RequestMapping("/eshop/banner/")
/**
 * 运营banner的设置
 * @author Administrator
 *
 */
public class EShopBannerController {

	@Autowired
	private PropertiesConfigure propertiesConfigure;
	@Autowired
	private EshopBannerService eshopBannerService;
	@Autowired
	private CommKeyService commKeyService;
	@Autowired
	private ImageUploadBizImpl imageUploadBizImpl;

	@RequestMapping("addBannerInfo")
	public String banner(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return "eshop/banner/addBannerInfo";

	}

	/**
	 * 富文本内容提交
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param bannerPics
	 */
	@RequestMapping("addBannerPicjson")
	@ResponseBody
	public void addDiscoverPic(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap,
			@RequestParam("bannerPics") MultipartFile[] bannerPics) {
		String uploadDir = propertiesConfigure.getProperties(PropertiesKey.userfilepath);
		uploadDir = uploadDir + "/eshopBanner";
		File dir = new File(uploadDir);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		ResponseEntity<List<String>> responseEntity = new ResponseEntity<>();
		List<String> picUrlList = new ArrayList<>();
		if (bannerPics != null && bannerPics.length > 0) {
			int picsLength = bannerPics.length;// 前段传值多了一张空图
			for (int i = 0; i < picsLength; i++) {// 这个是for循环减一
				String bannerPicUploadFileName = bannerPics[i].getOriginalFilename();
				if (StringUtils.isBlank(bannerPicUploadFileName)) {
					continue;
				}
				bannerPicUploadFileName = ImageUploadBizImpl.getFileNameAddTime(bannerPicUploadFileName);
				File bannerPicUploadFile = new File(dir, bannerPicUploadFileName);
				try {
					bannerPics[i].transferTo(bannerPicUploadFile);
				} catch (IllegalStateException | IOException e) {
				}
				String bannerPicUploadFilePath = "eshopBanner/" + bannerPicUploadFileName;

				picUrlList.add(propertiesConfigure.getProperties(PropertiesKey.userfileserver) + "/"
						+ bannerPicUploadFilePath);
			}
		}
		responseEntity.initSuccResponseEntity(picUrlList, "success");
		HttpUtil.ajaxJsonpOutput("", responseEntity, response);
	}

	/**
	 * 提交banner内容
	 * 
	 * @param request
	 * @param response
	 * @param mainPicPc
	 * @param mainPicH5
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("addEshopBannerAction")
	public String addBannerajax(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("mainPicPc") MultipartFile mainPicPc, @RequestParam("mainPicH5") MultipartFile mainPicH5,
			ModelMap modelMap) {
		
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		// User user = WebUtil.getCurrentUser(request);
		String title = HttpUtil.getStringValueFromRequest(request, "title");
		String bannerContent = request.getParameter("bannerContent");
		Integer bannerType = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "bannerType", 0);
		EshopBanner eshopBanner = new EshopBanner();
		eshopBanner.setGmtCreate(new Date());
		eshopBanner.setGmtModify(new Date());
		eshopBanner.setTitle(title);
		eshopBanner.setType(bannerType);
		eshopBanner.setStatus(0);
		eshopBanner.setCreateUserId(1l);
		// eshopBanner.setCreateUserId(user.getId());
		eshopBanner.setBannerDesc(bannerContent);// banner详情页的内容
		if (mainPicPc.getSize() != 0) {
			String pcImgFilePath = imageUploadBizImpl.uploadImage(mainPicPc, "eshopBanner", request);// pc端的banner图	
			eshopBanner.setMainPicPc(pcImgFilePath);
		}
		if (mainPicH5.getSize() != 0) {
			String h5ImgFilePath = imageUploadBizImpl.uploadImage(mainPicH5, "eshopBanner", request);// h5端的banner图
			eshopBanner.setMainPicH5(h5ImgFilePath);
		}
		List<Commkey> commKeyList = this.commKeyService.getComKeyByCommOnlyKey(onlyKey);
		eshopBanner.setCommOnlyKey(onlyKey);
		eshopBanner.setProvinceId(commKeyService.getRegionByList(RegionEnum.PROVINCE.getCode(), commKeyList));
		eshopBanner.setCityId(commKeyService.getRegionByList(RegionEnum.CITY.getCode(), commKeyList));
		eshopBanner.setAreaId(commKeyService.getRegionByList(RegionEnum.AREA.getCode(), commKeyList));

		Integer result = this.eshopBannerService.insertEshopBannerByPrimaryKey(eshopBanner);
		if (result != null && result > 0) {
			return "redirect:/eshop/banner/success.htm?bannerId="+eshopBanner.getId();
		}
		return "redirect:/eshop/banner/error.htm";
		
		
	}
	
	/**
	 * banner提交成功
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("success")
	public String success(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long bannerId = HttpUtil.getLongValueFromRequest(request, "bannerId");
		modelMap.put("bannerId", bannerId);
		return "eshop/banner/success";
	}
	/**
	 *提交失败
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("error")
	public String error(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		return "eshop/banner/error";
	}
	/**
	 * pc端的banner详情页
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("bannerInfoPc")
	public String bannerInfoPc(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		
		bannerInfoMap(request,response,modelMap);
		
		return "eshop/banner/bannerInfoPc";
	}
	/**
	 * h5端的banner详情页
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("bannerInfoH5")
	public String bannerInfoH5(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		bannerInfoMap(request,response,modelMap);
		return "eshop/banner/bannerInfoH5";
	}
	
	
	 

	/**
	 * banner列表
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("bannerList")
	public String bannerList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		bannerListMap(request,response,modelMap);
		return "eshop/banner/bannerList";
	}
	
	/**
	 * bannerlist加载更多
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("bannerMoreList")
	public String bannerMoreList(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		bannerListMap(request,response,modelMap);
		return "eshop/banner/bannerMoreList";
	}
	private ModelMap bannerListMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		Page page = new Page();
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize", PageUtil.PAGE_SIZE_APP);
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		Integer status = HttpUtil.getIntegerValueFromRequest(request, "status");
		List<EshopBanner> eshopBannerList = this.eshopBannerService.getEshopBannerListByPageAndStatus(page, status,0);
		modelMap.put("eshopBannerList", eshopBannerList);
		return modelMap;
	}
	
	private ModelMap bannerInfoMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		Long bannerId = HttpUtil.getLongValueFromRequest(request, "bannerId");
		EshopBanner eshopBanner = this.eshopBannerService.getEshopBannerById(bannerId);
		
		
		
		Page page = new Page();
		page.setPageByPageNoAndPageSize(1, PageUtil.PAGE_SIZE_APP);//公告栏右侧最多展示十条
		List<EshopBanner> eshopBannerList = this.eshopBannerService.getEshopBannerListByPageAndStatus(page, 1,eshopBanner.getType());
		modelMap.put("eshopBannerList", eshopBannerList);
		
		
		String reffer =  HttpUtil.getHttpReferer(request);
		modelMap.put("reffer", reffer);
		modelMap.put("eshopBanner", eshopBanner);
		return modelMap;
	}


	/**
	 * 修改banner图的状态
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("modifyBnnerAction")
	@ResponseBody
	public ResponseEntity<EshopBanner> modifyBnnerAction(HttpServletRequest request, HttpServletResponse response,
			ModelMap modelMap) {
		ResponseEntity<EshopBanner> responseEntity = new ResponseEntity<>();
		Integer status = HttpUtil.getIntegerValueFromRequest(request, "status");
		Long eshopBannerId = HttpUtil.getLongValueFromRequest(request, "eshopBannerId");
		if (eshopBannerId == null) {
			responseEntity.initErrorResponseEntity(null, "eshopBannerId is null");
		}
		EshopBanner eshopBanner = this.eshopBannerService.getEshopBannerById(eshopBannerId);
		if (eshopBanner == null) {
			responseEntity.initErrorResponseEntity(null, "eshopBanner is not exist");
		}
		eshopBanner.setStatus(status);
		eshopBanner.setGmtModify(new Date());
		Integer result = this.eshopBannerService.modifyEshopBannerByPrimaryKey(eshopBanner);
		if (result != null && result > 0) {
			responseEntity.initSuccResponseEntity(eshopBanner, "modify status success");
		}
		return responseEntity;
	}
	
	@RequestMapping("invitePartner")
	public String invitePartner(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
		return "eshop/invitePartner";
		
	}
	@RequestMapping("invitePartnerH5")
	public String invitePartnerH5(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
		return "eshop/invitePartnerH5";
		
	}

}
